package ru.mcfr.oxygen.framework.operations;

import ro.sync.ecss.extensions.api.*;
import ro.sync.ecss.extensions.api.node.AuthorDocumentFragment;
import ro.sync.ecss.extensions.api.node.AuthorElement;
import ro.sync.ecss.extensions.api.node.AuthorNode;
import ro.sync.util.URLUtil;

import javax.swing.*;
import javax.swing.text.BadLocationException;
import java.awt.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.URL;

/**
 * Operation to save the Author node at caret in a separate document and refresh the new file path in the project.
 */
public class ExtractNodeToFileOperation implements AuthorOperation {

    /**
     * @see ro.sync.ecss.extensions.api.AuthorOperation#doOperation(ro.sync.ecss.extensions.api.AuthorAccess, ro.sync.ecss.extensions.api.ArgumentsMap)
     */
    public void doOperation(AuthorAccess authorAccess, ArgumentsMap args) throws IllegalArgumentException,
            AuthorOperationException {
//        int caretOffset = authorAccess.getEditorAccess().getCaretOffset();
//        String message = null;
//        try {
//            // Get node at caret
//            AuthorNode nodeAtCaret = authorAccess.getDocumentController().getNodeAtOffset(caretOffset);
//            JFileChooser fileChooser = new JFileChooser();
//            fileChooser.setMultiSelectionEnabled(false);
//
//            // Show Save Dialog
//            if (fileChooser.showSaveDialog((Component) authorAccess.getWorkspaceAccess().getParentFrame())
//                    == JFileChooser.APPROVE_OPTION) {
//                File outputFile = fileChooser.getSelectedFile();
//                FileOutputStream fos = new FileOutputStream(outputFile);
//                OutputStreamWriter writer = new OutputStreamWriter(fos, "UTF-8");
//
//                // Write the node fragment
//                AuthorDocumentFragment fragment = authorAccess.getDocumentController().createDocumentFragment(nodeAtCaret, true);
//                String xmlFragment = authorAccess.getDocumentController().serializeFragmentToXML(fragment);
//                writer.write(xmlFragment);
//                writer.close();
//
//                // Open file
//                URL outputFileUrl = URLUtil.correct(outputFile);
//                authorAccess.getWorkspaceAccess().open(outputFileUrl);
//
//                // Refresh in project
//                authorAccess.getWorkspaceAccess().refreshInProject(outputFileUrl);
//            }
//        } catch (BadLocationException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//
//        if (message != null) {
//            authorAccess.getWorkspaceAccess().getWorkspaceAccess().showErrorMessage(message);
//        }
        try{
            AuthorNode[] nodes = authorAccess.getDocumentController().findNodesByXPath("//*", true, true, true);
            authorAccess.getWorkspaceAccess().showErrorMessage("elements: " + nodes.length);
            String ids = "value:\n";
            int maxID = 0;
            for (AuthorNode node : nodes){
                try{
                if (node.getType() == AuthorNode.NODE_TYPE_ELEMENT){
                    AuthorElement ae = (AuthorElement) node;
                    int id = Integer.valueOf(ae.getAttribute("id").getValue());
                    if (id > maxID)
                        maxID = id;
                    //ids += "name: " + ae.getXMLBaseURL().toString() + "id = " + ae.getAttribute("id") + "\n";
                }
                } catch (Exception e){

                }
            }

            authorAccess.getWorkspaceAccess().showErrorMessage("maxID: " + maxID);
        } catch (Exception e){
            authorAccess.getWorkspaceAccess().showErrorMessage("error:" + e.getMessage());
        }
    }

    /**
     * @see ro.sync.ecss.extensions.api.AuthorOperation#getArguments()
     */
    public ArgumentDescriptor[] getArguments() {
        return null;
    }

    /**
     * @see ro.sync.ecss.extensions.api.Extension#getDescription()
     */
    public String getDescription() {
        return "Save the Author node at caret in a separate document and refresh the new file path in the project";
    }

}
